package com.cssl.fmmall.service.imp;

import com.cssl.fmmall.dao.UsersMapper;
import com.cssl.fmmall.entity.Users;
import com.cssl.fmmall.service.UserService;
import com.cssl.fmmall.vo.ResStatus;
import com.cssl.fmmall.vo.Result;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class UserServiceImp implements UserService {
    @Resource
    private UsersMapper userMapper;

    @Override
    public Result userReg(String name, String pwd) {
        synchronized (this) {
            //1.根据用户查询，这个用户是否已经被注册
            Example example=new Example(Users.class);
            Example.Criteria criteria=example.createCriteria();
            criteria.andEqualTo("username",name);
            List<Users> users = userMapper.selectByExample(example);
            //2.如果没有被注册则进行保存操作
            if (users.isEmpty()) {
                Users user = new Users();
                user.setUsername(name);
                user.setPassword(pwd);
                user.setUserImg("images/default.png");
                user.setUserRegtime(new Date());
                user.setUserModtime((new Date()));
                int i = userMapper.insert(user);
                if (i > 0) {
                    return new Result(ResStatus.OK, "注册成功", null);
                } else {
                    return new Result(ResStatus.NO, "注册失败", null);
                }
            } else {
                return new Result(ResStatus.NO, "用户名已存在", null);
            }
        }
    }

    @Override
    public Result checkLogin(String name, String pwd) {
        Example example=new Example(Users.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("username",name);
        List<Users> users = userMapper.selectByExample(example);
        if(users.isEmpty()){
            return new Result(ResStatus.NO,"登陆失败，用户名不存在",null);
        }else{
            if(pwd.equals(users.get(0).getPassword())){
                //如果登录成功，则生成token,(使用jwt规则生成token字符串)
                JwtBuilder builder = Jwts.builder();
                HashMap map=new HashMap();
                String token=builder.setSubject(name)                                             //主题,token中携带的数据
                    .setIssuedAt(new Date())                                         //设置token的生成时间
                    .setId(users.get(0).getUserId()+"")                              //设置用户id为token的id
                    .setClaims(map)                                                   //map中存放用户的角色、权限信息
                    .setExpiration(new Date(System.currentTimeMillis()+24*60*60*1000)) //设置token的过期时间
                    .signWith(SignatureAlgorithm.HS256,"sbsbsbsb")                  //设置加密方式和加密密码
                    .compact();

                return  new Result(ResStatus.OK,token,users.get(0));

            }else{
                return new Result(ResStatus.NO,"登录失败，密码错误",null);
            }
        }
    }
}
